package com.escom.service;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.escom.healthlc.entidad.EstudioORM;
import com.escom.utils.Constantes;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by Carlos on 20/04/2014.
 */
public class EstudioService implements Runnable {

    private Handler handler;
    private String idPaciente;
    private String idMedico;

    public EstudioService(Handler handler, String idPaciente, String idMedico) {
        this.handler = handler;
        this.idPaciente = idPaciente;
        this.idMedico = idMedico;
    }

    @Override
    public void run() {
        Message msgObj = handler.obtainMessage();
        Bundle bundleMessage = new Bundle();
        int messageResponse = -1;
        Socket socket = null;
        try {
            socket = new Socket(Constantes.IP_RASPBERRY, Constantes.PORT_TCP);
            InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream();
            PrintWriter printWriter = new PrintWriter(outputStream, true);
            printWriter.println("START");

            ObjectInputStream is = new ObjectInputStream(inputStream);
            EstudioORM estudioORM = (EstudioORM) is.readObject();
            estudioORM.setIdMedico(idMedico);
            estudioORM.setIdPaciente(idPaciente);
            Log.d(Constantes.LOG_ID, this.getClass().getName() + " Size: " + estudioORM.getAirflows().size());
            Log.d(Constantes.LOG_ID, this.getClass().getName() + " Size: " + estudioORM.getArritmias().size());
            Log.d(Constantes.LOG_ID, this.getClass().getName() + " Size: " + estudioORM.getPositions().size());
            Log.d(Constantes.LOG_ID, this.getClass().getName() + " Size: " + estudioORM.getOximetros().size());
            Log.d(Constantes.LOG_ID, this.getClass().getName() + " Size: " + estudioORM.getTemperaturas().size());
            String directorio = "/storage/sdcard0/HealthLC";
            String archivo = directorio
                    + "/"
                    + new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(Calendar.getInstance().getTime())
                    + "-" + idMedico
                    + ".txt";

            File directorioFile = new File("/storage/sdcard0/HealthLC");
            if (!directorioFile.exists()) {
                boolean success = directorioFile.mkdir();
                if (success) {
                    Log.d(Constantes.LOG_ID, this.getClass().getName() + " Exito al crear el directorio");
                }
            }

            File archivoFile = new File(archivo);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(archivoFile));
            objectOutputStream.writeObject(estudioORM);
            objectOutputStream.close();


            Log.d(Constantes.LOG_ID, this.getClass().getName() + " Archivo creado");
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(Constantes.LOG_ID, this.getClass().getName() + " Error: " + e.fillInStackTrace().toString());
            messageResponse = Constantes.ERROR_COMUNICACION;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            Log.e(Constantes.LOG_ID, this.getClass().getName() + " Error: " + e.fillInStackTrace().toString());
            messageResponse = Constantes.ERROR_COMUNICACION;
        }

        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
                Log.e(Constantes.LOG_ID, this.getClass().getName() + " Error: " + e.fillInStackTrace().toString());
                messageResponse = Constantes.ERROR_COMUNICACION;
            }
        }

        if (messageResponse == -1) {
            messageResponse = Constantes.EXITO;
        }

        bundleMessage.putInt("message", messageResponse);
        msgObj.setData(bundleMessage);
        handler.sendMessage(msgObj);
    }
}
